﻿using AutoMapper;
using BaseData.API.Write.Applications.HospitalInformation.Command;
using BaseData.Domain.HospitalInformation;
using BaseData.ErrorCode;
using BaseData.Infrastructure.Interfaces;
using MediatR;

namespace BaseData.API.Write.Applications.HospitalInformation.Commandhandler
{
    public class DepartmentsUpdateCommandHandler : IRequestHandler<DepartmentsUpdateCommand,ApiResult<int>>
    {
        IBaseRepository<Departments> repository;
        IMapper mapper;

        public DepartmentsUpdateCommandHandler(IBaseRepository<Departments> repository, IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(DepartmentsUpdateCommand request, CancellationToken cancellationToken)
        {
            var res = repository.GetAll().FirstOrDefault (x => x.DepartmentId == request.DepartmentId);
            var quer = mapper.Map(request, res);
            await repository.UpdateAsync(quer);
            return new ApiResult<int>()
            {
                Message = ApiEnum.修改成功.ToString(),
                Code = ApiEnum.修改成功,
            };
        }
    }
}
